$(document).ready(function() {
    var page_wrap = $('#page-wrap');
    var page_tray = $('#page-tray');
    page_tray.width($('#page-content').width());
    setTimeout(function() {
        page_tray.addClass('transition');
    }, 100);
    $(window).resize(function() {
        page_tray.width(page_wrap.width() + (page_wrap.hasClass('show-user-panel') ? 225 : 0));
    });
    $('#user-panel-toggle').click(function() {
        if (page_wrap.hasClass('show-user-panel')) {
            page_tray.width(page_tray.width() - 225);
            page_wrap.removeClass('show-user-panel');

        } else {
            page_tray.width(page_tray.width() + 225);
            page_wrap.addClass('show-user-panel');

        }
    });
    //themes, change CSS with JS
    //default theme(CSS) is cerulean, change it if needed

    //ajax menu checkbox
    $('#is-ajax').click(function(e) {
        $.cookie('is-ajax', $(this).prop('checked'), {expires: 365});
    });
    $('#is-ajax').prop('checked', $.cookie('is-ajax') === 'true' ? true : false);

    //disbaling some functions for Internet Explorer
    if ($.browser.msie)
    {
        $('#is-ajax').prop('checked', false);
        $('#for-is-ajax').hide();
        $('#toggle-fullscreen').hide();
        $('.login-box').find('.input-large').removeClass('span10');

    }


    //highlight current / active link
    $('ul.main-menu li a').each(function() {
        if ($($(this))[0].href == String(window.location))
            $(this).parent().addClass('active');
    });

    //establish history variables
    var
            History = window.History, // Note: We are using a capital H instead of a lower h
            State = History.getState(),
            $log = $('#log');

    //bind to State Change
    History.Adapter.bind(window, 'statechange', function() { // Note: We are using statechange instead of popstate
        var State = History.getState(); // Note: We are using History.getState() instead of event.state
        $.ajax({
            url: State.url,
            success: function(msg) {
                $('#content').html($(msg).find('#content').html());
                $('#loading').remove();
                $('#content').fadeIn();
                var newTitle = $(msg).filter('title').text();
                $('title').text(newTitle);
                docReady();
            }
        });
    });

    //ajaxify menus
    $('a.ajax-link').click(function(e) {
        if ($.browser.msie)
            e.which = 1;
        if (e.which != 1 || !$('#is-ajax').prop('checked') || $(this).parent().hasClass('active'))
            return;
        e.preventDefault();
        if ($('.btn-navbar').is(':visible'))
        {
            $('.btn-navbar').click();
        }
        $('#loading').remove();
        $('#content').fadeOut().parent().append('<div id="loading" class="center">Loading...<div class="center"></div></div>');
        var $clink = $(this);
        History.pushState(null, null, $clink.attr('href'));
        $('ul.main-menu li.active').removeClass('active');
        $clink.parent('li').addClass('active');
    });

    //animating menus on hover
    $('ul.main-menu li:not(.nav-header)').hover(function() {
        $(this).animate({'margin-left': '+=5'}, 300);
    },
            function() {
                $(this).animate({'margin-left': '-=5'}, 300);
            });

    //other things to do on document ready, seperated for ajax calls
    docReady();
});
function docReady() {
    //prevent # links from moving to top
    $('a[href="#"][data-top!=true]').click(function(e) {
        e.preventDefault();
    });

    //rich text editor
    $('.cleditor').cleditor();
    //datepicker
    $('.datepicker').datepicker({dateFormat: "dd-mm-yy"});
    //chi nhap 0-9
    $('.numbersOnly').keyup(function() {
        this.value = this.value.replace(/[^0-9]/g, '');
    });
    //nhap 0-9\.
    $('.decimalOnly').keyup(function() {
        this.value = this.value.replace(/[^0-9]\./g, '');
    });
    //uniform - styler for checkbox, radio and file input
    $("input:checkbox, input:radio, input:file").not('[data-no-uniform="true"],#uniform-is-ajax').uniform();

    //chosen - improves select
    $('[data-rel="chosen"],[rel="chosen"]').chosen();

    //tabs
    $('#myTab a:first').tab('show');
    $('#myTab a').click(function(e) {
        e.preventDefault();
        $(this).tab('show');
    });


    //auto grow textarea
    $('textarea.autogrow').autogrow();


    //iOS / iPhone style toggle switch
    $('.iphone-toggle').iphoneStyle();




    $('.btn-close').click(function(e) {
        e.preventDefault();
        $(this).parent().parent().parent().fadeOut();
    });
    $('.btn-minimize').click(function(e) {
        e.preventDefault();
        var $target = $(this).parent().parent().next('.box-content');
        if ($target.is(':visible'))
            $('i', $(this)).removeClass('icon-chevron-up').addClass('icon-chevron-down');
        else
            $('i', $(this)).removeClass('icon-chevron-down').addClass('icon-chevron-up');
        $target.slideToggle();
    });
    $('.btn-setting').click(function(e) {
        e.preventDefault();
        $('#myModal').modal('show');
    });
}
function hideError(id) {
    setTimeout(function() {
        $(id).fadeOut('fast');
    }, 5000);
}
function actHideError(id_append, item) {
    item.css("color", "red");
    setTimeout(function() {
        $(id_append).fadeOut('fast');
    }, 5000);


}

function escapeHtml(text) {
    var characters = {
        '&': '&amp;',
        '"': '&quot;',
        "'": '&#039;',
        '<': '&lt;',
        '>': '&gt;'
    };
    return (text + "").replace(/[<>&"']/g, function(m){
        return characters[m];
    });
};